package com.windchime.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windchime.blog.entity.Blog;
import com.windchime.blog.entity.vo.BlogSearch;
import com.windchime.blog.entity.vo.status.EditAppreciate;
import com.windchime.blog.entity.vo.status.EditCommentable;
import com.windchime.blog.entity.vo.status.EditPublished;
import com.windchime.blog.mapper.BlogMapper;
import com.windchime.blog.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.windchime.blog.utils.MarkdownUtils;
import com.windchime.blog.utils.StringSplitUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 博客表 服务实现类
 * </p>
 *
 * @author WindChime
 * @since 2021-08-03
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    /**
     * 添加博客
     * @param blog 博客
     * @return true
     */
    @Override
    public boolean saveBlog(Blog blog) {
        blog.setViews(0);
        blog.setCommentCount(0);
        return baseMapper.insert(blog) > 0;
    }

    /**
     * 分页查询带条件
     * @param page       当前页
     * @param limit      当前页条目数
     * @param blogSearch 条件
     * @return blogs
     */
    @Override
    public PageInfo<Blog> selectBlogList(Integer page, Integer limit, BlogSearch blogSearch) {
        PageHelper.startPage(page, limit);
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if (!StringUtils.isEmpty(blogSearch.getTitle())) {
            wrapper.like("title", blogSearch.getTitle());
        }
        if (!StringUtils.isEmpty(blogSearch.getFlag())) {
            wrapper.eq("flag", blogSearch.getFlag());
        }
        if (!StringUtils.isEmpty(blogSearch.getPublished())) {
            wrapper.eq("published", blogSearch.getPublished());
        }
        List<Blog> blogs = baseMapper.selectList(wrapper);
        return new PageInfo<> (blogs);
    }

    /**
     * 修改发布状态
     * @param editPublished vo对象
     * @return true
     */
    @Override
    public boolean editPublished(EditPublished editPublished) {
        Blog blog = baseMapper.selectById(editPublished.getId());
        BeanUtils.copyProperties(editPublished, blog);
        return baseMapper.updateById(blog) > 0;
    }

    /**
     * 修改赞赏状态
     * @param editAppreciate vo对象
     * @return true
     */
    @Override
    public boolean editAppreciate(EditAppreciate editAppreciate) {
        Blog blog = baseMapper.selectById(editAppreciate.getId());
        BeanUtils.copyProperties(editAppreciate, blog);
        return baseMapper.updateById(blog) > 0;
    }

    /**
     * 修改评论状态
     * @param editCommentable vo对象
     * @return true
     */
    @Override
    public boolean editCommentable(EditCommentable editCommentable) {
        Blog blog = baseMapper.selectById(editCommentable.getId());
        BeanUtils.copyProperties(editCommentable, blog);
        return baseMapper.updateById(blog) > 0;
    }

    /**
     * 前台--查询最热博文
     * @return blogList
     */
    @Override
    public List<Blog> selectHotBlogs() {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("views");
        wrapper.last("limit 4");
        return baseMapper.selectList(wrapper);
    }

    /**
     * 前台--通过分类ID查询该分类有多少篇博客--需要发布后的
     * @param id 分类ID
     * @return num
     */
    @Override
    public Integer typeHasBlogsByTypeId(Integer id) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id", id);
        wrapper.eq("published", true);
        return baseMapper.selectCount(wrapper);
    }

    /**
     * 前台--设置每个分类ID下的博客
     * @param page  当前页
     * @param limit 当前页条目数
     * @param id    当前分类ID
     * @return blogs
     */
    @Override
    public PageInfo<Blog> selectBlogListByTypeId(Integer page, Integer limit, Integer id) {
        PageHelper.startPage(page, limit);
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id", id);
        List<Blog> blogs = baseMapper.selectList(wrapper);
        return new PageInfo<>(blogs);
    }

    /**
     * 前台--通过标签ID查询该标签有多少篇博客--需要发布后的
     * @param id 标签ID
     * @return num
     */
    @Override
    public Integer tagHasBlogsByTagId(Integer id) {
        return baseMapper.selectCountTagHasBlogs(id);
    }

    /**
     * 设置以年归档
     * @return archiveBlogs
     */
    @Override
    public Map<String, List<Blog>> archiveBlog() {
        //归档时间
        List<String> years = baseMapper.findGroupYear();

        Map<String, List<Blog>> map = new HashMap<>();
        for (String year : years) {
            //根据年份查询博客
            List<Blog> blogs = baseMapper.findBlogsByYear(year);
            map.put(year + "年", blogs);
        }
        return map;
    }

    /**
     * 前台--搜索服务所需分页
     * @param page  当前页
     * @param limit 当前页条目数
     * @param query 搜索条件
     * @return blogs
     */
    @Override
    public PageInfo<Blog> selectBlogListByQuery(Integer page, Integer limit, String query) {
        PageHelper.startPage(page, limit);
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("views");
        wrapper.eq("published", true);
        if (!StringUtils.isEmpty(query)) {
            wrapper.like("title", query);
        }
        List<Blog> blogs = baseMapper.selectList(wrapper);
        return new PageInfo<> (blogs);
    }

    /**
     * 根据ID查询博客--内容通过markdown工具转换
     * @param id 博客ID
     * @return blog 博客对象
     */
    @Override
    public Blog getBlogForConvert(Long id) {
        Blog blog = baseMapper.selectById(id);
        /*
            更新浏览次数
         */
        int views = blog.getViews();
        blog.setViews(views + 1);
        this.updateById(blog);

        Blog b = new Blog();
        BeanUtils.copyProperties(blog, b);
        String content = b.getContent();
        b.setContent(MarkdownUtils.markdownToHtmlExtensions(content));

        return b;
    }

    /**
     * 前台--设置每个标签ID下的博客
     * @param page  当前页
     * @param limit 当前页条目数
     * @param id    当前标签ID
     * @return blogs
     */
    @Override
    public PageInfo<Blog> selectBlogListByTagId(Integer page, Integer limit, Integer id) {
        PageHelper.startPage(page, limit);
        List<Blog> blogs = baseMapper.tagHasBlogs(id);
        return new PageInfo<>(blogs);
    }
}
